class Api::V1::UserSearchKeysController < Api::V1::BaseController
  def index
    @search_keys = @current_user.user_search_keys.order("created_at DESC").page(params[:page]).per(params[:per])
  end

  def create
    @search_key = UserSearchKey.where(user_id: @current_user.id, search_key: params[:search_key]).first
    if @search_key.presence
      @search_key.search_times += 1
    else
      @search_key = UserSearchKey.new
      @search_key.user_id = @current_user.id
      @search_key.search_key = params[:search_key]
      @search_key.search_times = 1
    end
    if @search_key.save
      render json: { result: true, message: '记录成功.' }, status: :ok
    else
      render json: { result: false, message: '记录失败.' }, status: :ok
    end
  end
  
  def destroy
    @search_key = UserSearchKey.find params[:id]
    if @search_key.destroy
      render json: { result: true, message: '删除成功.' }, status: :ok
    else
      render json: { result: false, message: '删除失败.' }, status: :ok
    end
  end
end
